/*
 * Copyright (C) 2009-2018 Hangzhou Iroly Technology Co., Ltd. All rights reserved
 */
package com.aliyun.messagemgt.common.dto.response;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * Date: 2019-02-13
 * Time: 15:38
 * Description:
 *
 * @author qiezi
 */
@Data
public class ResultMap<T> extends HashMap {

    /**
     * 接口调用是否成功，0：成功，1：失败
     */
    public static final String ERROR_CODE = "errCode";
    public static final String CODE = "code";
    /**
     * 业务编码 接口调用错误业务编码
     */
    public static final String ERROR_MESSAGE = "errMsg";
    /**
     * 数据对象 成功需要返回消息，在data里加一个属性（比如：“showMsg”）
     */
    public static final String DATA = "datas";
    public static final String DATA1 = "data";

    /**
     * 异常
     */
    public static final int ERROR = 1;
    /**
     * 成功
     */
    public static final int SUCCESS = 0;
    public static final String SUCCESS_MESSAGE = "success";

    private static String TOTAL_COUNT = "totalCount";
    private static String PAGE_NUM = "pageNum";
    private static String PAGE_SIZE = "pageSize";
    private static String PAGE_TOTAL = "pages";

    public ResultMap(T t) {
        //初始化默认为成功
        setSuccess(true);
        //如果传入的是result对象类型，则将result对象转换为ResultMap对象,如还有其他转换对象，可以在下面再加一个else,我们尽量统一远程dubbo接口返回对象为Result
        if (t instanceof Result) {
            Result result = (Result) t;
            //当接口调用失败时，返回 errorCode、message两个字段
            if (!result.isSuccess()) {
                put(ERROR_CODE, ERROR);
                put(ERROR_CODE, result.getResultCode());
                put(ERROR_MESSAGE, result.getMessage());
                put(SUCCESS_MESSAGE, false);
            }
            if (null != result.getModel()) {
                put(DATA, result.getModel());
            }
            //当对象为Map时。属于特殊情况， 不需要放到key为data的属性中，直接copy返回
        } else if (t instanceof Map) {
            putAll((Map) t);
        } else {
            put(DATA, t);
        }
    }

    public ResultMap(int errorCode, String message) {
        put(ERROR_CODE, ERROR);
        put(ERROR_CODE, errorCode);
        put(ERROR_MESSAGE, message);
    }

    public ResultMap(int code, String msg, T data) {
        put(CODE, code);
        put(DATA1, data);
        put(ERROR_MESSAGE, msg);
    }

    public ResultMap() {
        setSuccess(true);
        put(ERROR_CODE, SUCCESS);
        put(ERROR_MESSAGE, SUCCESS_MESSAGE);//初始化默认 为 成功
    }

    public ResultMap(boolean success) {
        put(ERROR_CODE, success ? SUCCESS : ERROR);
    }

    public static ResultMap success(Object object) {
        ResultMap resultMap = new ResultMap();
        resultMap.setSuccess(true);
        resultMap.setErrMsg("success");
        resultMap.setData(object);
        resultMap.setCode(HttpStatus.OK.value());
        // 如果是Page类型，默认附带上分页等一系列参数
        if (object instanceof Page) {
            Page page = (Page) object;
            resultMap.put(PAGE_SIZE, page.getPageSize());
            resultMap.put(PAGE_NUM, page.getPageNum());
            resultMap.put(TOTAL_COUNT, (int) page.getTotal());
            resultMap.put(PAGE_TOTAL, page.getPages());
            resultMap.setData(page.getResult());
        } else if (object instanceof PageInfo) {
            PageInfo pageInfo = (PageInfo) object;
            resultMap.put(PAGE_SIZE, pageInfo.getPageSize());
            resultMap.put(PAGE_NUM, pageInfo.getPageNum());
            resultMap.put(TOTAL_COUNT, (int) pageInfo.getTotal());
            resultMap.put(PAGE_TOTAL, (int) pageInfo.getPages());
            resultMap.setData(pageInfo.getList());
        } else if (object instanceof AjaxResult) {
            AjaxResult ajaxResult = (AjaxResult) object;
            resultMap.put(PAGE_SIZE, ajaxResult.getPageSize());
            resultMap.put(PAGE_NUM, ajaxResult.getPageNum());
            resultMap.put(TOTAL_COUNT, (int) ajaxResult.getTotalCount());
            resultMap.put(PAGE_TOTAL, (int) ajaxResult.getPages());
            resultMap.setData(ajaxResult.getDatas());
        } else {
            return resultMap;
        }
        return resultMap;
    }

    public void setCode(int code) {
        put(CODE, code);
    }

    public void setSuccess(boolean success) {
        put(ERROR_CODE, success ? SUCCESS : ERROR);
        put(SUCCESS_MESSAGE, success);
    }

    public void setErrCode(int errCode) {
        put(ERROR_CODE, errCode);
    }

    public void setErrMsg(String errMsg) {
        put(ERROR_MESSAGE, errMsg);
    }

    public void setData(T t) {
        put(DATA, t);
    }

    public void setTotalCount(int totalCount) {
        put(TOTAL_COUNT, totalCount);
    }

    public void setPageNum(int pageNum) {
        put(PAGE_NUM, pageNum);
    }

    public static <T> ResultMap error(int code, String msg, T data) {
        return new ResultMap(code, msg, data);
    }
}

